package com.ascent.service;

import com.ascent.dao.OrderitemMapper;
import com.ascent.dao.OrdersMapper;
import com.ascent.classes.Orderitem;
import com.ascent.classes.Orders;
import com.ascent.classes.Product;
import com.ascent.utils.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 处理订单管理相关的类（删除、修改和询等）
 */
public class OrderService {
    SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();

    /**
     * 查询所有订单信息
     *
     * @return
     */
    public List<Orders> selectAllOrder() {
        // 获取Mapper
        SqlSession sqlSession = sqlSessionFactory.openSession();
        OrdersMapper mapper = sqlSession.getMapper(OrdersMapper.class);
        //调用方法
        List<Orders> orders = mapper.selectAllOrder();
        //释放资源
        sqlSession.close();
        // 返回订单集合
        return orders;
    }

    /**
     * 根据订单id查询订单项，返回商品id
     *
     * @param ordersid
     * @return
     */
    public List<Product> selectOrderitemByOrdersid(int ordersid) {
        // 获取Mapper
        SqlSession sqlSession = sqlSessionFactory.openSession();
        OrderitemMapper mapper = sqlSession.getMapper(OrderitemMapper.class);
        //调用方法
        int[] productid = mapper.selectOrderitemProductidByOrdersid(ordersid);
        //调用其他接口的商品方法，通过商品id获得商品参数
        List<Product> products = new ArrayList<>();
        ProductService productService = new ProductService();
        try {
            for (int i = 0; i < productid.length; i++) {
                Product product = null;

                product = productService.queryById(productid[i]);

                products.add(product);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        //释放资源
        sqlSession.close();
        // 返回订单项集合
        return products;
    }

    /**
     * 删除订单项根据商品id和订单id
     *
     * @param productid
     * @param ordersid
     */
    public void deleteOrderitemByproductidAndordersid(int productid, int ordersid) {
        // 获取Mapper
        SqlSession sqlSession = sqlSessionFactory.openSession();
        OrderitemMapper mapper = sqlSession.getMapper(OrderitemMapper.class);
        //调用方法
        mapper.deleteOrderitemByproductidAndordersid(productid, ordersid);
        //提交事务和释放资源
        sqlSession.commit();
        sqlSession.close();
    }

    public Orderitem selectOrderitemByproductidAndordersid(int productid, int ordersid) {
        // 获取Mapper
        SqlSession sqlSession = sqlSessionFactory.openSession();
        OrderitemMapper mapper = sqlSession.getMapper(OrderitemMapper.class);
        //调用方法
        Orderitem orderitem = mapper.selectOrderitemByproductidAndordersid(productid, ordersid);
        //释放资源
        sqlSession.close();
        // 返回订单项
        return orderitem;
    }

    /**
     * 删除订单根据订单id
     *
     * @param id
     */
    public void deleteOrderByid(int id) {
        // 获取Mapper
        SqlSession sqlSession = sqlSessionFactory.openSession();
        OrdersMapper mapper = sqlSession.getMapper(OrdersMapper.class);
        //调用方法删除订单
        mapper.deleteOrderByid(id);
        //提交事务和释放资源
        sqlSession.commit();
        sqlSession.close();
    }

    /**
     * 查询订单根据订单id
     *
     * @param id
     */
    public Orders selectOrderByid(int id) {
        // 获取Mapper
        SqlSession sqlSession = sqlSessionFactory.openSession();
        OrdersMapper mapper = sqlSession.getMapper(OrdersMapper.class);
        //调用方法查询订单
        Orders orders = mapper.selectOrderByid(id);
        //释放资源
        sqlSession.close();
        // 返回订单
        return orders;
    }

    /**
     * 删除订单内的订单项根据其订单id
     *
     * @param ordersid
     */
    public void deleteOrderitemByordersid(int ordersid) {
        // 获取Mapper
        SqlSession sqlSession = sqlSessionFactory.openSession();
        OrderitemMapper mapper = sqlSession.getMapper(OrderitemMapper.class);
        //调用方法
        mapper.deleteOrderitemByordersid(ordersid);
        //提交事务和释放资源
        sqlSession.commit();
        sqlSession.close();
    }

    public List<Orders> selectOrderByusrid(int usrid) {
        // 获取Mapper
        SqlSession sqlSession = sqlSessionFactory.openSession();
        OrdersMapper mapper = sqlSession.getMapper(OrdersMapper.class);
        //调用方法查询订单
        List<Orders> orders = mapper.selectOrderByusrid(usrid);
        //释放资源
        sqlSession.close();
        // 返回订单
        return orders;
    }
}
